package com.feishi.core.util.timer;

/**
 * Created by caixq on 4/17 017.
 */
public class CycleExecutor {
    public static void execute(Executable exec ){
        execute(exec,new ExecuteRule(500,1.2f,-1));
    }
    public static void execute(Executable exec,ExecuteRule rule ){
        long sleepTime=rule.getSleepTime();
        float coefficient=rule.getCoefficient();
        int maxTime=rule.getMaxTime();
        int executeTime=0;

        while ((maxTime==-1||maxTime-executeTime-->=0)&&!exec.execute()){
            sleep(sleepTime);
            sleepTime*=coefficient;
        }
    }

    private static void sleep(long sleepTime) {
        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
